<?php

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Comment_MxShopPage extends WebPage {

    public function main() {
        global $_W, $_GPC;
        $this -> orderList(1, 'main');
    }

    public function check(){
        global $_W, $_GPC;
        $this -> orderList(0, 'check');
    }

    protected function orderlist($check, $action){

        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $condition = ' c.deleted=0';

        $paras = array();

        $orderstatus = array(1 => array('css' => 'danger', 'name' => '待付款'), 2 => array('css' => 'warning', 'name' => '进行中'), 3 => array('css' => 'success', 'name' => '已完成'), 4 => array('css' => 'default', 'name' => '已关闭'), 5 => array('css' => 'danger', 'name' => '维权申请'), 6 => array('css' => 'default', 'name' => '维权完成',));

        if($check != 1){
            $condition .= ' AND c.checked = :checked';
            $paras[':checked'] = $check;
        }


        if (!(empty($_GPC['searchfield'])) && !(empty($_GPC['keyword'])))
        {
            $paras[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            if($_GPC['searchfield'] == 'ordersn'){
                $condition .= ' AND locate(:keyword,o.ordersn)>0';

            } else if ($_GPC['searchfield'] == 'member'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0)';

            } else if ($_GPC['searchfield'] == 'goods'){
                $condition .= ' AND locate(:keyword,g.name)>0 ';

            }else if ($_GPC['searchfield'] == 'mendian'){
                $condition .= ' AND locate(:keyword,d.mendianname)>0 ';
            }
        }

        $comment = array('1'=>'差评', '2'=>'一般', '3'=>'挺好', '4'=>'满意', '5'=>'非常满意');

        $list = pdo_fetchall('SELECT c.*, o.ordersn as ordersn, m.avatar as avatar, m.mobile as mobile, m.realname as realname, m.id as mid FROM ' . tablename('mx_shop_service_order_comment') . ' c LEFT JOIN ' .tablename('mx_shop_user') . ' m ON c.uid=m.id LEFT JOIN '.tablename('mx_shop_service_order').' o ON c.orderid=o.id LEFT JOIN '.tablename('mx_shop_service_goods').' g ON c.goodsid=g.id LEFT JOIN '.tablename('mx_shop_mendian').' d ON c.mendianid=d.id WHERE ' . $condition . ' ORDER BY c.id DESC LIMIT '. (($pindex - 1) * $psize) . ',' . $psize, $paras);
        foreach ($list as $key => $item) {
            $list[$key]['images'] = unserialize($item['images']);
        }
        $total = pdo_fetchcolumn('SELECT count(c.id) FROM ' . tablename('mx_shop_service_order_comment') . ' c LEFT JOIN ' .tablename('mx_shop_user') . ' m ON c.uid=m.id LEFT JOIN '.tablename('mx_shop_service_order').' o ON c.orderid=o.id LEFT JOIN '.tablename('mx_shop_service_goods').' g ON c.goodsid=g.id LEFT JOIN '.tablename('mx_shop_mendian').' d ON c.mendianid=d.id WHERE ' . $condition, $paras);

        $pager = pagination($total, $pindex, $psize);

        include $this->template('sorder/comment');

    }

    public function pass(){
        global $_W, $_GPC;

        $id = $_GPC['id'];
        if(empty($id)){
            show_json(0, '参数有误！');
        }

        plog('sorder.comment.pass', '评价审核通过 ID: ' . $id);

        pdo_update('mx_shop_service_order_comment', array('checked'=>1), array('id'=>$id));

        show_json(1);
    }

    public function del(){
        global $_W, $_GPC;

        $id = $_GPC['id'];
        if(empty($id)){
            show_json(0, '参数有误！');
        }

        plog('sorder.comment.del', '评价删除 ID: ' . $id);

        pdo_update('mx_shop_service_order_comment', array('deleted'=>1), array('id'=>$id));

        show_json(1);
    }

}
